package com.yqy.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqy.entity.StudentGrade;
import com.yqy.entity.StudentInfo;
import com.yqy.mapper.StudentGradeMapper;
import com.yqy.mapper.StudentInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author yiqianyi
 * @date 2022/3/18 16:25
 */
@RestController
@CrossOrigin
@Api(tags = {"学生信息控制层"})
public class StudentInfoController {
  private static final Logger log = LoggerFactory.getLogger(StudentInfoController.class);
  @Autowired private StudentInfoMapper studentInfoMapper;
  @Autowired private StudentGradeMapper studentGradeMapper;

  @GetMapping("/student/info/byAccount")
  @ApiOperation(value = "根据账号获取个人信息", notes = "/student/info/byAccount")
  public StudentInfo findStudentInfoByAccount(
      @RequestParam @ApiParam(name = "account账号", value = "account", required = true)
          String account) {
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.eq("account", account);
    return studentInfoMapper.selectOne(queryWrapper);
  }

  @PostMapping("/student/info/getAll")
  @ApiOperation(value = "获取所有信息", notes = "/student/info/getAll")
  public List<StudentInfo> getAllStudentInfo() {
    return studentInfoMapper.selectList(null);
  }

  @PostMapping("/student/info/getPage")
  @ApiOperation(value = "分页获取信息", notes = "/student/info/getPage")
  public IPage<StudentInfo> getPageStudentInfo(
      @RequestParam @ApiParam(name = "pageNum", value = "pageNum", required = true) int pageNum,
      @RequestParam @ApiParam(name = "pageSize", value = "pageSzie", required = true) int pageSize,
      @RequestBody @ApiParam(name = "studentInfo", value = "studentInfo", required = true)
          StudentInfo studentInfo) {
    QueryWrapper queryWrapper = new QueryWrapper();
    if (!("").equals(studentInfo.getUsername())) {
      queryWrapper.like("username", studentInfo.getUsername());
    }
    if (!("").equals(studentInfo.getSex())) {
      queryWrapper.eq("sex", studentInfo.getSex());
    }
    if (!("").equals(studentInfo.getRemark())) {
      queryWrapper.like("remark", studentInfo.getRemark());
    }
    Page<StudentInfo> studentInfoPage = new Page<>(pageNum, pageSize);
    return studentInfoMapper.selectPage(studentInfoPage, queryWrapper);
  }

  @PostMapping("/studentinfo/export")
  @ApiOperation(value = "导出学生信息 response", notes = "/studentinfo/export")
  public void export(
      HttpServletResponse response,
      @RequestBody @ApiParam(name = "studentInfo", value = "studentInfo", required = true)
          StudentInfo studentInfo) {
    try {
      // 设置响应输出的头类型
      response.setHeader("content-Type", "application/vnd.ms-excel");
      // 下载文件的默认名称
      response.setHeader("Content-Disposition", "attachment;filename=user.xls");
      // =========easypoi部分
      ExportParams exportParams = new ExportParams();
      // exportParams.setDataHanlder(null);//和导入一样可以设置一个handler来处理特殊数据
      Workbook workbook =
          ExcelExportUtil.exportExcel(exportParams, StudentInfo.class, getData(studentInfo));
      workbook.write(response.getOutputStream());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }
  }

  @GetMapping("/studentinfo/getExport")
  @ApiOperation(value = "导出学生信息(响应response)", notes = "/studentinfo/getExport")
  public void getExport(HttpServletResponse response) {
    try {
      // 设置响应输出的头类型
      response.setHeader("content-Type", "application/vnd.ms-excel");
      // 下载文件的默认名称
      response.setHeader("Content-Disposition", "attachment;filename=user.xls");
      // =========easypoi部分
      ExportParams exportParams = new ExportParams();
      // exportParams.setDataHanlder(null);//和导入一样可以设置一个handler来处理特殊数据
      Workbook workbook =
          ExcelExportUtil.exportExcel(
              exportParams, StudentInfo.class, studentInfoMapper.selectList(null));
      workbook.write(response.getOutputStream());
      response.getOutputStream().close();
      workbook.close();
    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }
  }

  @GetMapping("/studentinfo/getExport1")
  @ApiOperation(value = "导出学生信息", notes = "/studentinfo/getExport1")
  public int getExport1() {
    try {
      ExportParams exportParams = new ExportParams();
      Workbook workbook =
          ExcelExportUtil.exportExcel(
              exportParams, StudentInfo.class, studentInfoMapper.selectList(null));
      SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String filename =
          "/StudentInfo/学生信息列表"
              + sdf1.format(new Date()).replace("-", "").replace(":", "").replace(" ", "")
              + ".xls";
      System.out.println(filename);
      // FileOutputStream outputStream = new FileOutputStream("./学生信息列表20220319182023.xls");
      FileOutputStream outputStream =
          new FileOutputStream("D:/毕业设计/service-yqy-student" + filename);
      workbook.write(outputStream);
      workbook.close();
      return 1;
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return 0;
    }
  }

  private List<StudentInfo> getData(StudentInfo studentInfo) {
    QueryWrapper queryWrapper = new QueryWrapper();
    if (!("").equals(studentInfo.getUsername())) {
      queryWrapper.like("username", studentInfo.getUsername());
    }
    if (!("").equals(studentInfo.getSex())) {
      queryWrapper.eq("sex", studentInfo.getSex());
    }
    if (!("").equals(studentInfo.getRemark())) {
      queryWrapper.like("remark", studentInfo.getRemark());
    }
    return studentInfoMapper.selectList(queryWrapper);
  }

  @PostMapping("/student/info/update")
  @ApiOperation(value = "更新学生信息", notes = "/student/info/update")
  public int update(
      @RequestBody @ApiParam(name = "student", value = "user", required = true)
          StudentInfo studentInfo) {
    StudentGrade studentGrade = new StudentGrade();
    studentGrade.setIsClass(studentInfo.getIsClass());
    studentGrade.setUsername(studentInfo.getUsername());
    studentGradeMapper.updateByIsClass(studentGrade);
    return studentInfoMapper.updateById(studentInfo);
  }

  @PostMapping("/student/info/delete")
  @ApiOperation(value = "批量删除学生信息", notes = "/student/info/delete")
  public int delete(
      @RequestParam @ApiParam(name = "ids", value = "1,2,3", required = true) String ids) {
    String[] split = ids.split(",");
    List<String> list = Arrays.asList(split);
    return studentInfoMapper.deleteBatchIds(list);
  }
}
